﻿using Method.DateTime;
using Models;
using Models.RModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RedisHelper.User
{
    public class UserStatistics
    {
        /// <summary>
        /// 获取用户关注数量
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public long GetPayCount(int uid)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashLength(StaticConfigRidesName.UserPay_Hash(uid));
            }
        }

        /// <summary>
        /// 获取用户关注列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<R_UserPay> GetPayList(int uid)
        {
            List<R_UserPay> list = new List<R_UserPay>();
            using (RedisHelper rh = new RedisHelper())
            {
                var data = rh.GetDatabase(4).HashGetAll(StaticConfigRidesName.UserPay_Hash(uid));
                data.ToList().ForEach(u => list.Add(new R_UserPay()
                {
                    UserId = Convert.ToInt32(u.Name),
                    Time = u.Value
                }));
                return list;
            }
        }

        /// <summary>
        /// 获取用户粉丝列表
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public List<R_UserPay> Get_PayList(int uid)
        {
            List<R_UserPay> list = new List<R_UserPay>();
            using (RedisHelper rh = new RedisHelper())
            {
                var data = rh.GetDatabase(4).HashGetAll(StaticConfigRidesName.UserPayPassive_Hash(uid));
                data.ToList().ForEach(u => list.Add(new R_UserPay()
                {
                    UserId = Convert.ToInt32(u.Name),
                    Time = u.Value
                }));
                return list;
            }
        }

        /// <summary>
        /// 获取用户粉丝数量
        /// </summary>
        /// <param name="uid"></param>
        /// <returns></returns>
        public long Get_PayCount(int uid)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashLength(StaticConfigRidesName.UserPayPassive_Hash(uid));
            }
        }

        /// <summary>
        /// 获取用户是否关注另一个用户
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curid"></param>
        /// <returns></returns>
        public bool GetMutualPay(int uid, int curid)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                var data = rh.GetDatabase(4).HashGet(StaticConfigRidesName.UserPay_Hash(uid), curid);
                if (data.IsNull)
                    return false;
                return true;
            }
        }

        /// <summary>
        /// 插入用户关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool InsertUserPay(int uid, int curId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashSet(StaticConfigRidesName.UserPay_Hash(uid), curId, GetDateID.getDateId);
            }
        }

        /// <summary>
        /// 插入用户粉丝
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool InsertUser_Pay(int uid, int curId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashSet(StaticConfigRidesName.UserPayPassive_Hash(uid), curId, GetDateID.getDateId);
            }
        }

        /// <summary>
        /// 删除用户关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool DelUserPay(int uid, int curId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashDelete(StaticConfigRidesName.UserPay_Hash(uid), curId);
            }
        }

        /// <summary>
        /// 删除用户粉丝
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool DelUser_Pay(int uid, int curId)
        {
            using (RedisHelper rh = new RedisHelper())
            {
                return rh.GetDatabase(4).HashDelete(StaticConfigRidesName.UserPayPassive_Hash(uid), curId);
            }
        }

        /// <summary>
        /// 用户关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool UserPay(int uid, int curId)
        {
            using (RedisHelper th = new RedisHelper())
            {
                var database = th.GetDatabase(4);

                var tran = database.CreateTransaction();
                tran.HashSetAsync(StaticConfigRidesName.UserPay_Hash(uid), curId, GetDateID.getDateId);
                tran.HashSetAsync(StaticConfigRidesName.UserPayPassive_Hash(curId), uid, GetDateID.getDateId);

                return tran.Execute();
            }
        }

        /// <summary>
        /// 用户取消关注
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="curId"></param>
        /// <returns></returns>
        public bool UserCancelPay(int uid, int curId)
        {
            using (RedisHelper th = new RedisHelper())
            {
                var database = th.GetDatabase(4);

                var tran = database.CreateTransaction();
                tran.HashDeleteAsync(StaticConfigRidesName.UserPay_Hash(uid), curId);
                tran.HashDeleteAsync(StaticConfigRidesName.UserPayPassive_Hash(curId), uid);
                return tran.Execute();
            }
        }
    }
}
